package com.test.A12;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

import com.test.A12.A12.Foo;

/*****
 * Proxy类的作用 内置了一个protected InvocationHandler h属性  并且构造方法已经初始化了
 *
 */
public class $Proxy1  extends Proxy implements Foo {
 
	
	public $Proxy1(InvocationHandler invocationHandler) {
		 super(invocationHandler);
	}

	@Override
	public void foo()  {
		try {
			h.invoke(this,foo, new Object[0]);
		} catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        } 
	}

	@Override
	public int bar() {
		try {
		     Object result = h.invoke(this,bar, new Object[0]);
		   return   (int) result;
		  } catch (RuntimeException | Error e) {//运行时异常或者错误  直接抛出
	            throw e;
	        } catch (Throwable e) {//检查异常 需要转换UndeclaredThrowableException(运行时异常的子类)抛出   
	            throw new UndeclaredThrowableException(e);
	        }
	}
	
	 static Method foo;
	 static Method bar;
	    static {
	        try {
	            foo = A12.Foo.class.getMethod("foo");
	            bar = A12.Foo.class.getMethod("bar");
	        } catch (NoSuchMethodException e) {  //静态代码块的异常必须捕获 
	            throw new NoSuchMethodError(e.getMessage());
	        }
	    }

}
